# regenerate json schema after changing yaml files

import yaml
import json
import sys

folder = sys.argv[1]

with open(f'{folder}/metrics.yaml') as f:
    metrics = yaml.load(f, Loader=yaml.FullLoader)
with open(f'{folder}/pings.yaml') as f:
    pings = yaml.load(f, Loader=yaml.FullLoader)


schema = {'pings': [k for k in pings.keys() if k != '$schema'],
          'metrics': {c: {name: {'type': info['type'],
                                 'send_in_pings': info['send_in_pings']}
                          for name, info in m.items()}
                      for c, m in metrics.items() if c != '$schema'}}

with open(f'{folder}/schema.js', 'w') as f:
    f.write("/* eslint-disable */\n"
            "// this file is autogenerated, run python scripts/update-telemetry-schema.py to regenerate after yaml file was changed\n"
            "const telemetrySchema = " + json.dumps(schema, indent=4))




